import { defineStore } from 'pinia';
import * as actypes from '../types'
import {
  loadSearch,
  loadPlay,
  loadFavorite,
  saveSearch,
  savePlay,
  saveFavorite,
  deleteSearch,
  deletePlay,
  deleteFavorite,
  clearSearch,
  clearPlay,
  clearFavorite
} from '@/assets/js/cache';

export const usePlayerStore = defineStore('player', {
  state: () => {
    return {
      miniPlayer: true,
      fullPlayer: false,
      /**
       * audio
       */
      audioReady: false,
      audioPlayState: false,
      audioPlayTime: 0,
      audioTotalTime: 360,
      /**
       * song
       */
      currentPlayMode: 0,
      currentPlayIndex: -1,
      currentPlayList: [] as Array<any>,
      currentPlayHistory: loadPlay(),
      currentSearchHistory: loadSearch(),
      currentFavoriteList: loadFavorite(),
      // 顺序的列表
      sequenceList: [] as Array<any>,
      /**
       * lyric
       */
      lyric: null as any,
      lyricIndex: 0,
      lyricIndexTxt: ''
    };
  },
  getters: {},
  actions: {
    [actypes.SET_MINI_PLAYER](payload: boolean) {
      this.miniPlayer = payload;
    },
    [actypes.SET_FULL_PLAYER](payload: boolean) {
      this.fullPlayer = payload;
    },
    /**
     * audio
     */
    [actypes.SET_AUDIO_READY](payload: boolean) {
      this.audioReady = payload;
    },
    [actypes.SET_AUDIO_PLAY_STATE](payload: boolean) {
      this.audioPlayState = payload;
    },
    [actypes.SET_AUDIO_PLAY_TIME](payload: number) {
      this.audioPlayTime = payload;
    },
    [actypes.SET_AUDIO_TOTAL_TIME](payload: number) {
      this.audioTotalTime = payload;
    },
    /**
     * song
     */
    [actypes.SET_CURRENT_PLAY_MODE](payload: number) {
      this.currentPlayMode = payload;
    },
    [actypes.SET_CURRENT_PLAY_INDEX](payload: number) {
      this.currentPlayIndex = payload;
    },
    [actypes.SET_CURRENT_PLAY_LIST](payload: Array<any>) {
      this.currentPlayList = payload;
    },
    [actypes.SET_SEQUENCE_LIST](payload: Array<any>) {
      this.sequenceList = payload;
    },
    [actypes.SET_CURRENT_PLAY_HISTORY](payload: any) {
      this.currentPlayHistory = savePlay(payload);
    },
    [actypes.SET_CURRENT_SEARCH_HISTORY](payload: any) {
      this.currentSearchHistory = saveSearch(payload);
    },
    [actypes.SET_CURRENT_FAVORITE_LIST](payload: any) {
      this.currentFavoriteList = saveFavorite(payload);
    },
    [actypes.ADD_TO_PLAY_HISTORY](payload: any) {
      this.currentPlayHistory = savePlay(payload);
    },
    [actypes.ADD_TO_SEARCH_HISTORY](payload: any) {
      this.currentSearchHistory = saveSearch(payload);
    },
    [actypes.ADD_TO_FAVORITE_LIST](payload: any) {
      this.currentFavoriteList = saveFavorite(payload);
    },
    // 添加歌曲队列
    [actypes.ADD_TO_PLAY_LIST](payload: any) {
      // 使用原数据的副本
      let playlist = this.currentPlayList.slice();
      // 查找待填加的歌曲是否存在,存在的话就直接返回
      let pdIndex = playlist.findIndex(item => item.id == payload.id);
      if (pdIndex > -1) return;
      playlist.push(payload);
      this.SET_CURRENT_PLAY_LIST(playlist);
      this.SET_SEQUENCE_LIST(playlist);
    },
    // 添加歌曲到下一首播放
    [actypes.ADD_TO_NEXT_PLAY](payload: any) {
      // 使用原数据的副本
      let playlist = this.currentPlayList.slice();
      let currentIndex = this.currentPlayIndex;
      // 先在下个位置插入歌曲
      currentIndex++;
      playlist.splice(currentIndex, 0, payload);
      // 查找待填加的歌曲是否存在，存在就删除
      let pdIndex = playlist.findIndex(item => item.id == payload.id);
      if (pdIndex > -1) {
        if (pdIndex < currentIndex) {
          playlist.splice(pdIndex, 1);
          currentIndex--;
        } else {
          playlist.splice(pdIndex + 1, 1);
        }
      }
      this.SET_CURRENT_PLAY_LIST(playlist);
      this.SET_SEQUENCE_LIST(playlist);
    },
    // 删除搜索记录
    [actypes.DEL_CURRENT_SEARCH_HISTORY](isAll: boolean, payload: any) {
      if (isAll) {
        clearSearch();
      }
      deleteSearch(payload);
      this.currentSearchHistory = loadSearch();
    },
    // 删除播放记录
    [actypes.DEL_CURRENT_PLAY_HISTORY](isAll: boolean, payload: any) {
      if (isAll) {
        clearPlay();
      }
      deletePlay(payload);
      this.currentPlayHistory = loadPlay();
    },
    // 删除收藏记录
    [actypes.DEL_CURRENT_FAVORITE_LIST](isAll: boolean, payload: any) {
      if (isAll) {
        clearFavorite();
      }
      deleteFavorite(payload);
      this.currentFavoriteList = loadFavorite();
    },
    /**
     * lyric
     */
    [actypes.SET_LYRIC](payload: any) {
      this.lyric = payload;
    },
    [actypes.SET_LYRIC_INDEX](payload: number) {
      this.lyricIndex = payload;
    },
    [actypes.SET_LYRIC_INDEX_TXT](payload: string) {
      this.lyricIndexTxt = payload;
    }
  }
});
